//方法函数
/*
function getsum(num) {
    let sum = 0;
    for (let i = 0; i <= parseInt(num); i++) {
        sum += i;
    }
    alert("本月应该存："+sum+"块钱！！！");
}

let num = prompt("请输入本月的天数:")
getsum(num);
*/

//-----------------------------------------------
/*
function getmax(arr) {
    let max = arr[0];
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] > max) {
            max = arr[i]
        }
    }
    return max;
}

let re = getmax([1, 2, 3, 4, 5, 6, 7, 8])
alert(re);
*/
//-----------------------------------------------
//arguments,只有函数才有，每个函数都内置好的。
/*
function fu() {
    for (let i = 0; i <= arguments.length; i++) {
        alert(arguments[i]);
    }
}

fu(1, 2, 3, 4, 5, 6, 7);
*/
//-----------------------------------------------
//数组元素翻转
/*
function reverse(arr) {
    if (arr instanceof Array) {//判断是不是数组
        let newArr = [];
        for (let i = arr.length - 1; i >= 0; i--) {
            newArr[newArr.length] = arr[i];
        }
        alert(newArr);
    } else {
        return "error这个参数要求必须为数组格式【1，2，3，4....】"
    }
}

let arr1 = reverse([1, 2, 3, 4, 5, 6]);
alert(arr1);
*/
//-----------------------------------------------
//用户输入年份判断2月份的天数
/*
function backDay() {
    let inputYear = prompt("请输入年份：");
    if (isyear(inputYear)) {
        alert("是闰年")
    } else {
        alert("是平年")
    }
}

backDay();

function isyear(year) {
    let flag = false;
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
        flag = true;
    }
    return flag;
}
*/
//-----------------------------------------------
//对象
/*
let obj = {
    name: "古力娜扎",
    age: 90,
    sex: "女",
    method: function () {
        alert("method");
    }
}
alert(obj.age);
alert(obj["name"]);
obj.method();
*/
//-----------------------------------------------
//new 对象
/*
let obj = new object();
obj.name = "古力娜扎";
obj.age = 90;
obj.sex = "女";
obj.method = function () {
    alert("method");
}
alert(obj.age);
alert(obj["name"]);
obj.method();
*/
//-----------------------------------------------
//new 对象
/*
function Star(name, sex, age) {
    this.name = name;
    this.sex = sex;
    this.age = age;
}

let ldh = new Star("luidehua", "nan", 19);
alert(ldh.name);
*/
//-----------------------------------------------
//全部输出对象属性值
/*
let obj = {
    name: "古力娜扎",
    age: 90,
    sex: "女",
    method: function () {
        alert("method");
    }
}
for (let i in obj) {
    alert(i);
    alert(obj[i]);
}
*/
//-----------------------------------------------
//内置对象
/*
alert(Math.PI);
alert(Math.max(1, 2, 3, 4, 5, 6, 7, 8, 9));
alert(Math.random() * 10);
*/
//-----------------------------------------------
//封装对象并引用
/*
let mymath = {
    pi: 3.14,
    max: function () {
        let max = arguments[0];
        for (let i = 1; i < arguments.length; i++) {
            if (max < arguments[i]) {
                max = arguments[i];
            }
        }
        return max;
    },
    mix: function () {
        let mix = arguments[0];
        for (let i = 1; i < arguments.length; i++) {
            if (mix > arguments[i]) {
                mix = arguments[i];
            }
        }
        return mix;
    }
}
alert(mymath.pi);
alert(mymath.mix(1, 2, 3, 4, 5));
alert(mymath.max(1, 2, 3, 4, 5));
*/
//-----------------------------------------------
//随机整数
/*
function getRandom(max, mix) {
    return Math.floor(Math.random() * (max - mix - 1)) + mix;
}

alert(getRandom(1, 10));
let arr = ["张三", "李四", "王五", "赵六"]
alert(arr[getRandom(0, arr.length - 1)]);
*/

//-----------------------------------------------
//猜数字
/*function getRandom(max, mix) {
    return Math.floor(Math.random() * (max - mix - 1)) + mix;
}

let random = getRandom(1, 50);
for (let num = 0; num <= 10; num++) {
    let print = prompt("输入你猜出来的数字");
    if (print > random) {
        alert("你猜的太大了！");
    } else if (print < random) {
        alert("你猜的太小了！")
    } else {
        alert("你猜对了！");
        break;
    }
}
alert(getRandom(1, 10));*/
//-----------------------------------------------
//日期对象
/*
function getTime() {
    let time = new Date();//如果没有参数，返回当前时间；
    let year = time.getFullYear();
    let month = time.getMonth() + 1;
    month = month < 10 ? "0" + month : month;
    let times = time.getDate();
    times = times < 10 ? "0" + times : times;
    let arr = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
    let day = time.getDay();
    let hour = time.getHours();
    hour = hour < 10 ? "0" + hour : hour;
    let minutes = time.getMinutes();
    let seconds = time.getSeconds();
    seconds = seconds < 10 ? "0" + seconds : seconds;
    alert("今天是" + year + "年" + month + "月" + times + "日" + arr[day]
        + hour + "点" + minutes + "分" + seconds + "秒");
    //第一种方法
    alert(time.valueOf());//现在时间距离1970年1月1日的总的毫秒数
    alert(time.getTime());//现在时间距离1970年1月1日的总的毫秒数
    //第二种方法
    let time1 = +new Date();//+new 就是返回总的毫秒数
    alert(time1);
    //H5中新增的第三种方法
    alert(Date.now());
}

getTime();
*/
//-----------------------------------------------
//倒计时算法
/*
function conutDown(time) {
    let nowTime = +new Date();//返回当前时间的总毫秒数
    let inputTime = +new Date(time);//返回用户输入的时间总毫秒数
    let times = (nowTime - inputTime) / 1000;//剩余时间总的毫秒数
    let day = parseInt(times / 60 / 60 / 24);
    day = day < 10 ? "0" + day : day;
    let hour = parseInt(times / 60 / 60 % 24);
    hour = hour < 10 ? "0" + hour : hour;
    let minutes = parseInt(times / 60 % 60);
    minutes = minutes < 10 ? "0" + minutes : minutes;
    let seconds = parseInt(times % 60);
    seconds = seconds < 10 ? "0" + seconds : seconds;
    return alert(day + "天" + hour + "时" + minutes + "分" + seconds + "秒");
}

let print = prompt("请输入现在时间：\n" + "格式为：XXXX-XX-XX XX：XX:XX");
conutDown(print);
*/
//-----------------------------------------------
//数组
/*
let arr = [1, 2, 3, 4, 5, 6];
alert(Array.isArray(arr)); //判断是不是数组
//数组的结尾添加数组元素
arr.push("zifuchuan", "shuzi", 7);
alert(arr);//返回数组长度
//数组的开头添加数组元素
arr.unshift("zifuchuan", "shuzi", 7);
alert(arr);//返回数组长度
alert(arr.pop());//删除最后一个数组元素
alert(arr.shift());//删除第一个数组元素
*/
//-----------------------------------------------
/*
let arr = [1, 2, 3, 4, 5, 6];
let newArray = [];
for (let i = 0; i < arr.length; i++) {
    if (arr[i] < 4) {
        newArray.push(arr[i]);//满足条件的依次追加到新数组中
    }
}
alert(newArray);
*/
//-----------------------------------------------
//数组升序降序排序
/*
let arr = [2, 3, 6, 1, 8, 4, 12];
arr.sort(function (a, b) {
    // return a - b;//升序
    return b - a;//降序
})
alert(arr);
*/
//-----------------------------------------------
//索引号
/*
let arr = [2, 3, 6, 1, 8, 4, 12];
//从前往后
alert( arr.indexOf(8));
//从后往前
alert(arr.lastIndexOf(2));
*/

//-----------------------------------------------
//数组去重
/*
function unique(arr) {
    let newArr = [];
    for (let i = 0; i < arr.length; i++) {
        if (newArr.indexOf(arr[i]) === -1) {
            newArr.push(arr[i]);
        }
    }
    return newArr;
}

var demo = unique([1, 1, 1, 1, 1, 2, 3, 3, 3, 4, 4, 5, 6, 6, 7, 7, 8, 8, 9]);
alert(demo);
*/
//-----------------------------------------------
//数组转换字符串
/*
let arr = [1, 2, 3];
alert(arr.toString());
alert(arr.join("-"));//横杠链接字符串
*/
//-----------------------------------------------
//查找字符
/*
let str = "qwyertyuiop";
alert(str.indexOf("y", 3));
*/
//-----------------------------------------------
/*
let str = "rtyguhjicvbhjqwr";
let num = 0
var index = str.indexOf("j");
while (index !== -1) {
    alert(index);
    num++;
    index = str.indexOf("j", index + 1);
}
alert(num);
*/
//-----------------------------------------------
/*
let str = "qwyertyuiop";
alert(str.charAt(3));
for (let i = 0; i < arr.length; i++) {
    alert(str.charAt(i));
}
alert(str.charCodeAt(0));
alert(str[0]);
*/
//-----------------------------------------------
//判断对象是否有该属性
/*
let man = {
    name: "xiaoming ",
    age: 18
}
if (man["sex"]) {
    alert("有该属性");
} else {
    alert("没有该属性。");
}
*/

//-----------------------------------------------
//统计字符串
/*
let str = "qwyertyuiop";
let o = {};
for (let i = 0; i < str.length; i++) {
    let chars = str.charAt(i);//chars是字符串的每一个字符
    if (o[chars]) {
        o[chars]++;
    } else {
        o[chars] = 1;
    }
}
alert(o);
let max = 0;
let ch = "";
for (let k in o) {
    if (o[k] > max) {
        max = o[k];
        ch = k;
    }
}
alert(max, ch);
*/
//-----------------------------------------------
//substr,截取字符串，第一个截取几个
/*
let str = "gaigechunfengchuimandi";
alert(str.substr(2, 2));
*/
//-----------------------------------------------
//替换字符串,只会替换第一个
/*
let str = "gaigechunfengchuimandi";
alert(str.replace("qaige", "qiege"));
while (str.indexOf("o") !== -1) {
    str = str.replace("qaige", "qiege");
}
alert(str);
//字符串转换为数组
let str = "gaig,ec,hunfengchuim,andi";
alert(str.split("."));
*/